package pe.integracion;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import pe.integracion.config.CMISFacadeAbstract;
import pe.integracion.config.Constantes;


@WebService(targetNamespace = "http://ecm-ws.com.pe", portName = "IntegracionECMPort", serviceName = "IntegracionECMService")
@SOAPBinding(style = Style.RPC)
public class IntegracionECMImpl extends CMISFacadeAbstract  {

	public IntegracionECMImpl() {

	}

	@WebMethod(operationName = "existDirectory")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "EdRes")	
	public Retorno existDirectory(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "folderName", mode = Mode.IN) String folderName) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.existDirectory(folderName);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "createDirectory")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "CdRes")
	public Retorno createDirectory(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "folderName", mode = Mode.IN) String folderName) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.createDirectory(folderName);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "existDocument")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "EdocRes")
	public Retorno existDocument(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "uuid", mode = Mode.IN) String uuid) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.existDocument(uuid);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "createDocument")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "MdRes")
	public Retorno createDocument(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "file", mode = Mode.IN) byte[] file,
			@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "directory", mode = Mode.IN) String directory,
			@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "fileName", mode = Mode.IN) String fileName,
			@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "mimetype", mode = Mode.IN) String mimetype) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.createDocument(file, directory, fileName, mimetype);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "getDocument")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "GdocRes")
	public Retorno getDocument(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "uuid", mode = Mode.IN) String uuid) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.getDocument(uuid);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "deleteDocument")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "DdocRes")
	public Retorno deleteDocument(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "uuid", mode = Mode.IN)  String uuid) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.deleteDocument(uuid);
		} else {
			return retorno;
		}
	}

	@WebMethod(operationName = "deleteDirectory")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "DdRes")
	public Retorno deleteDirectory(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "folderName", mode = Mode.IN)String folderName) {
		
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.deleteDirectory(folderName);
		} else {
			return retorno;
		}
	}
	
	@WebMethod(operationName = "queryForContent")
	@RequestWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "java.lang.String")
	@ResponseWrapper(targetNamespace = "http://ecm-ws.com.pe/ws/types", className = "pe.integracion.Retorno")
	@WebResult(targetNamespace = "http://ecm-ws.com.pe/ws/types", name = "qFcResponse")
	public Retorno queryForContent(@WebParam(targetNamespace = "http://ecm-ws.com.pe/ws/ws/types", name = "patron", mode = Mode.IN)String patron) {
		Retorno retorno = this.validacionWSS();
		if ("1".equals(""+retorno.getCodigo())){
			return super.queryForContent(patron);
		} else {
			return retorno;
		}		
	}

	@Resource
	private WebServiceContext webServiceContext;
	
	private Retorno validacionWSS(){           
	      System.out.println( "-------- DENTRO DE: [validacionWSS] --------" ); 
	      Retorno retorno = null; 
	      MessageContext objMessageContext = null; 
		
		  Map<Object,Object> objCabeceraRequest = null;  
		  List<Object> objListaUsuarios    = null;
		  List<Object> objListaPasswords   = null;
				   
		  String vUsuarioWSS  = "";  
		  String vPasswordWSS = "";
		   
		  boolean vMensajeValidacionWSS = false;
		   
        try{
	      	  objMessageContext = this.webServiceContext.getMessageContext();
	
	    	  //------------ OBTENSION DATOS CABECERA 'REQUEST': ------------//
	    	  objCabeceraRequest = (Map<Object,Object>)objMessageContext.get( MessageContext.HTTP_REQUEST_HEADERS );
	    	  objListaUsuarios   = (List<Object>)objCabeceraRequest.get( "username" );
	    	  objListaPasswords  = (List<Object>)objCabeceraRequest.get( "password" );
	    	  //------------------------------------------------------------//
	    	  
	    	  //Obteniendo: USUARIO.
	    	  if( objListaUsuarios != null ){
	    		  vUsuarioWSS = objListaUsuarios.get( 0 ).toString();	    		  
	    	  } else {
	    		  retorno = new Retorno(0, "Usuario no puede ser nulo.");
	    	  }
	    	   
	    	  //Obteniendo: PASSWORD.
	    	  if( objListaPasswords != null ){
	    	      vPasswordWSS = objListaPasswords.get( 0 ).toString();	    	      	    	      
	    	  } else {
	    		  retorno = new Retorno(0, "Clave no puede ser nula.");
	    	  }
	
	    	  System.out.println( "- vUsuarioWSS: ["  + vUsuarioWSS  + "]" );
	    	  System.out.println( "- vPasswordWSS: [" + vPasswordWSS + "]" );
	    	  
	    	  //ESTA PARTE PUEDE SER OBTENIDA DE UN .PROPERTIES Y/O BD:
	    	  if( vUsuarioWSS.equals(  Constantes.USUARIO_WSS  ) && 
	    		  vPasswordWSS.equals( Constantes.PASSWORD_WSS ) ){				    		  	    		  
	    		  retorno = new Retorno(1, "Credenciales Validas");	    		  
	    	  } 
	    	  else{				    		   
	    		   retorno = new Retorno(0, "Credenciales invalidas");
	    	  }
        }
        catch( Exception e ){
               throw e;
        }
         
        return retorno;		
	}

	
}
